package cn.bigfire.wedesk;

import cn.bigfire.wedesk.bean.Wedesk;
import cn.bigfire.wedesk.net.initializer.TcpClientChannelInitializer;
import cn.bigfire.wedesk.net.protocol.TypeMapper;
import cn.bigfire.wedesk.store.Stores;
import cn.bigfire.wedesk.util.PCInfoUtil;
import cn.bigfire.wedesk.util.PackUtil;
import cn.bigfire.wedesk.util.ScreenSendTimer;
import cn.bigfire.wedesk.util.TcpUtil;
import cn.bigfire.wedesk.util.ScreenTimer;
import cn.hutool.core.swing.ScreenUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;

import java.util.Scanner;


/**
 * 局域网发现
 * https://blog.51cto.com/u_14448891/10511905
 *
 * 自己的想法是，
 * 1.客户端直接有个定时器，去看大火服务器和自己局域网内是否又可能用TCP服务器，如果发现直接连接，并上班本机信息
 * 2.虽说客户端自己会找服务器。但是客户端也需要开放一个端口。来方便局域网服务器连接客户端。
 * 目前想用的两个端口  服务器8354  客户端8808
 *
 */
public class WeDeskClient {

    public static String cmdStr = "";
    static {
        StringBuilder sb = new StringBuilder();
        for (Wedesk.Cmd cmd : Wedesk.Cmd.values()) {
            String name = cmd.name().replace("CMD_","");
            int value = cmd.getValue();
            if (value == 0 || value > 500) {
                continue;
            }
            sb.append(name).append(":").append(value).append(",");
        }
        cmdStr = sb.toString();

        ScreenUtil.captureScreen();//初始化截屏工具类，加速首次截图
        ScreenTimer.init();
        ScreenSendTimer.init();
    }

    public static void main(String[] args) {
        String ip = "127.0.0.1";
//        String ip = "192.168.0.245";
//        String ip = "bigfire.cn";
        int port = 54320;
        System.out.println(JSON.toJSONString(PCInfoUtil.pcInfo));//打印当前设备信息
        Channel channel = TcpUtil.clientConnect(ip, port, new TcpClientChannelInitializer());
        if (channel == null) {
            System.err.println("连接失败,程序终止");
            return;
        }
        Stores.channel = channel;
        PackUtil.channel = channel;
        System.out.println("成功注册类型:" + JSON.toJSONString(TypeMapper.type2ClassMap.keySet()));


        //直接发送设备信息
        PackUtil.pcInfo();

        new Thread(() -> {
            Scanner scanner = new Scanner(System.in); // 创建Scanner对象
            while (true) {
                try {
                    ThreadUtil.sleep(20);
                    System.out.println("请先输入操作-1:exit," + cmdStr);
                    String input = scanner.nextLine();
                    int opCode = Integer.parseInt(input);
                    if (Wedesk.Cmd.CMD_PING.getValue() == opCode) {
                        PackUtil.ping();
                    } else if (Wedesk.Cmd.CMD_HEART.getValue() == opCode) {
                        PackUtil.heart();
                    } else if (Wedesk.Cmd.CMD_REGISTER.getValue() == opCode) {
                        System.out.println("用户名：");
                        String username = scanner.nextLine();
                        System.out.println("密码：");
                        String password = scanner.nextLine();
                        System.out.println("密码2：");
                        String password2 = scanner.nextLine();
                        Wedesk.RegisterReq registerReq = new Wedesk.RegisterReq();
                        registerReq.setUsername(username);
                        registerReq.setPassword(password);
                        registerReq.setPassword2(password2);
                        PackUtil.register(registerReq);
                    } else if (Wedesk.Cmd.CMD_LOGIN.getValue() == opCode) {
                        System.out.println("用户名：");
                        String username = scanner.nextLine();
                        System.out.println("密码：");
                        String password = scanner.nextLine();
                        Wedesk.LoginReq loginReq = new Wedesk.LoginReq();
                        loginReq.setUsername(username);
                        loginReq.setPassword(password);
                        loginReq.setPcInfo(PCInfoUtil.pcInfo);
                        PackUtil.login(loginReq);
                    } else if (Wedesk.Cmd.CMD_CTRL_LIST.getValue() == opCode) {
                        PackUtil.ctrlList();
                    } else if (Wedesk.Cmd.CMD_CTRL_OPEN.getValue() == opCode) {
                        System.out.println("cid：");
                        String cid = scanner.nextLine();
                        Wedesk.Ctrl ctrl = new Wedesk.Ctrl();
                        ctrl.setToCid(cid);
                        PackUtil.ctrlOpen(ctrl);
                    } else if (Wedesk.Cmd.CMD_CTRL_CLOSE.getValue() == opCode) {
                        System.out.println("cid：");
                        String cid = scanner.nextLine();
                        Wedesk.Ctrl ctrl = new Wedesk.Ctrl();
                        ctrl.setToCid(cid);
                        PackUtil.ctrlClose(ctrl);
                    } else if (opCode == -1) {
                        System.exit(0);
                    } else {
                        System.err.println("未知操作，请重新输入");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }



}
